package log4j.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;

import com.demo.persistencia.generica.modelo.Log4jdata;

@Controller
@RequestMapping("/main")
public class ControllerSpringJasper {
	
	private static Logger log =  Logger.getLogger(ControllerSpringJasper.class);
	
	public ControllerSpringJasper() {
		log.info("Estoy en el Constructor  Orale ControllerSpringJasper");
	}
	
    @RequestMapping(value="/download/reportPDF.htm", method=RequestMethod.GET)
    public String generarReporteLogPDF(ModelMap modelMap, HttpServletResponse response, HttpServletRequest request) {
      
    	List<Log4jdata> lista = new ArrayList<Log4jdata>();
        HttpSession session=request.getSession(true);
	    if(session.getAttribute("lstLogger")!=null){
	    	lista  = (List<Log4jdata>) session.getAttribute("lstLogger");
		}
        response.setHeader("Content-type", "application/pdf");
        response.setHeader("Content-Disposition","attachment; filename=\"ReporteLoggerPDF.pdf\"");
        JRBeanCollectionDataSource jrbean = new JRBeanCollectionDataSource(lista, false);
        modelMap.put("dataKey", jrbean);
        try {
        	OutputStream os = response.getOutputStream();
		    os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
        return("pdfReport");
    }

    @RequestMapping(value="/download/xlsReport.htm", method=RequestMethod.GET)
    public String generarReporteLogXLS(ModelMap modelMap, HttpServletResponse response, HttpServletRequest request) {
      
    	List<Log4jdata> lista = new ArrayList<Log4jdata>();
        HttpSession session=request.getSession(true);
	    if(session.getAttribute("lstLogger")!=null){
	    	lista  = (List<Log4jdata>) session.getAttribute("lstLogger");
		}
        response.setHeader("Content-type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition","attachment; filename=\"ReporteLoggerXLS.xls\"");
        JRBeanCollectionDataSource jrbean = new JRBeanCollectionDataSource(lista, false);
        modelMap.put("dataKey", jrbean);
        try {
        	OutputStream os = response.getOutputStream();
		    os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
        return("xlsReport");
    }
    

}
